package com.car.carmgr.controller;

import com.car.carmgr.bean.Customer;
import com.car.carmgr.service.CustomerService;
import com.car.carmgr.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("business")
public class CustomerController {
    
    @Autowired
    private CustomerService customerService;

    @RequestMapping("customer/customerRightJson")
    public AjaxResult customerRightJson(String identity, String customerName, Integer sex, String address, String phone, String career, Date createTime, Integer page, Integer limit){
        PageInfo<Customer> pageInfo = customerService.findCustomerByWhere(identity, customerName, sex,  address,  phone,  career,  createTime,page, limit);
        Integer count = customerService.getCountByWhere(identity, customerName, sex,  address,  phone,  career,  createTime);
        return AjaxResult.ok(count,pageInfo.getList());
    }

    @RequestMapping("customer/customerAddOrUpdate")
    public AjaxResult customerAddOrUpdate(Customer customer){

        int row = customerService.findCustomerById(customer.getIdentity());//判断数据裤中是否有此身份证号
        if(row == 0){
            customer.setCreateTime(new Date());
            int i = customerService.insertCustomer(customer);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else{
                return AjaxResult.ADD_FAILED();
            }
        }else{
            int i = customerService.updateCustomer(customer);
            if(i>0){
                return AjaxResult.UPD_SUCCESS();
            }else{
                return AjaxResult.UPD_FAILED();
            }
        }
    }

    @RequestMapping("customer/customerDelete")
    public AjaxResult customerDelete(Integer id){
        int i = customerService.deleteCustomerById(id);
        if(i>0){
            return AjaxResult.DEL_SUCCESS();
        }else{
            return AjaxResult.DEL_FAILED();
        }
    }

    @RequestMapping("customer/deleteSomeCustomer")
    public AjaxResult deleteSomeCustomer(String customerIds){
        int i = customerService.deleteSomeCustomer(customerIds.split(","));
        if(i>0){
            return AjaxResult.DELSOME_SUCCESS();
        }else{
            return AjaxResult.DELSOME_FAILED();
        }
    }
}
